package net.oschina.dajiangnan.wx.qr;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


import net.oschina.dajiangnan.wx.constants.Config;
import net.oschina.dajiangnan.wx.process.pay.bean.Product;
import net.oschina.dajiangnan.wx.qr.serializable.Products;
import net.oschina.dajiangnan.wx.util.*;


public class Qrcode {

    private static final String qrext = "png";
    private static final int qrwidth = 400;
    private static final int qrheight = 400;
    private static final String qrworkpath = PathUtil.getJarOrTargetPath(PathUtil.class) + "qrdata/";
    private static final String qrworkdata = "data";
    
    private static Products products;
    
    static {
    	if(products == null){
            if(new File(qrworkpath + qrworkdata).exists()){
                products = (Products)SerialUtil.FileSystemToObject(qrworkpath + qrworkdata);
            }else{
                products = new Products();
                SerialUtil.ObjectToFileSystem(products, qrworkpath + qrworkdata);
            }
        }
    }
    
    
    
    
    private static String makeQrCode(String productid){
        
        long time_stamp = System.currentTimeMillis()/1000;
        String nonce_str = RandomUtil.getRandomStr(32);
        
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("appid", Config.appid);
        map.put("mch_id", Config.mch_id);
        map.put("nonce_str", nonce_str);
        map.put("product_id", productid);
        map.put("time_stamp", time_stamp);
        String apiKey = Config.apikey;
        
        String sign = Signature.getSign(map, apiKey);
        
        String url = "weixin://wxpay/bizpayurl?" +
                "appid="+Config.appid+
                "&mch_id="+Config.mch_id+
                "&nonce_str="+nonce_str+
                "&product_id="+productid+
                "&time_stamp="+time_stamp+
                "&sign="+sign;
        
        try {
            QRCode.generate(productid, qrext, url, qrworkpath,qrwidth,qrheight);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return qrworkpath + productid + "." + qrext;
    }



    
    
    public static String addQrCodeConfig(String productid,String body,String detail,int fee) throws AlreadExistQRCodeException{
        if(products.getProduct(productid) != null){
            throw new AlreadExistQRCodeException();
        }else{
            Product product = new Product();
            product.setProductid(productid);
            product.setBody(body);
            product.setDetail(detail);
            product.setFee(fee);
            products.getProducts().add(product);
            SerialUtil.ObjectToFileSystem(products, qrworkpath + qrworkdata);
            return makeQrCode(productid);
        }
    }
    public static boolean delQrCodeConfig(String productid){
    	String deleteImg = qrworkpath+productid+"."+qrext;
    	if(products.getProduct(productid) == null
    			||!new File(deleteImg).exists()){
    		return false;
    	}
    	Product product = new Product();
    	product.setProductid(productid);
    	products.getProducts().remove(product);
        SerialUtil.ObjectToFileSystem(products, qrworkpath + qrworkdata);
        return new File(deleteImg).delete();
    }
    public static Product getProduct(String productid){
        return products.getProduct(productid);
    }
    
    
    
    
    
    
    public static class AlreadExistQRCodeException extends Exception{
        private static final long serialVersionUID = -6549946359422595233L;
        
        public AlreadExistQRCodeException(){
            super();
        }
    }
    
}
